package com.xiaomei.manager.web.page;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag<T> extends TagSupport {
	  
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Page<T> page;  
	
    @SuppressWarnings("static-access")
	public int doStartTag() {  
    	//标签开始时调用的方法   
        try {  
        	JspWriter out = pageContext.getOut();  
	        if (page == null){  
	        	out.print("没有记录");  
	            out.flush();  
	            return super.SKIP_BODY;  
	        }  
            
	        out.print("第 " + page.getPageNo() + " 页/共 " + (page.getTotalPage()) + "页, 每页显示: <input type='text' name='pageSize' value='" + page.getPageSize() + "' size='2' />");  
	        out.print("<input type='button' value='确定' onclick='submitForm()' />&nbsp;&nbsp;&nbsp;"); 
	        if(page.getPageNo() == 1) {  
	        	out.print("[首页]");  
	        	out.print("[上一页]");  
	        } else {
	        	out.print("[<a href='#' onclick='setPageNo(1)'>首页</a>]");  
                out.print("[<a href='#' onclick='setPageNo(" + (page.getPageNo() - 1) + ")'>上一页</a>]"); 
	        }
	  
            for  (int i = page.getPageNo()-3; i <= page.getPageNo()+3; i++) {  
            	if(i <= 0  || i > page.getTotalPage()) {  
            		continue ;  
            	}  
            	if(i == page.getPageNo()) { 
            		out.print("<input type='hidden' id='pageNo' name='pageNo' value= '" + page.getPageNo() + "' />");
            		out.print( "<span style='color:#FF0000; border: 1px solid #cccccc; font-weight:bold; width:15px;text-align: center;'> "   
	                                    + i +  " </span>" );  
            	}  else  {  
            		out.print( "[<a href='#' onclick='setPageNo(" + i + ")'> "  + i +  " </a>]" );  
            	}  
            }
            
            if(page.getPageNo() == page.getTotalPage() || page.getTotalPage() <= 0) {  
            	out.print( "[下一页]" );  
            	out.print( "[尾页]" );  
            } else { 
                out.print( "[<a href='#' onclick='setPageNo(" + (page.getPageNo() + 1) + ")'>下一页</a>]");  
                out.print( "[<a href='#' onclick='setPageNo(" + page.getTotalPage() + ")'>尾页</a>]");  
	        }  
            
            out.flush();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
	    return super.SKIP_BODY; //表示不显示标签间的文字   
    }  
	  
	@SuppressWarnings ( "static-access" )  
	public int doEndTag() {  
		//标签结束时调用的方法   
		return super.EVAL_PAGE; //表示JSP页面继续运行   
	}  
	  
	public void setPage(Page<T> page) {  
		this .page = page;  
	}  
	  
	public Page<T> getPage() {  
		return  page;  
	}  
}  
